package com.baijia.authentication.principal.credentials;

import com.baijia.authentication.AccountType;
import com.baijia.authentication.principal.Credentials;
import com.baijia.common.enums.UserRoleType;

import org.apache.commons.lang.StringUtils;

import javax.validation.constraints.NotNull;

/**
 * @title JigouCredentials
 * @desc 机构登录凭证
 * @author caoliang
 * @date 2015年11月11日
 * @version 1.0
 */
public class JigouCredentials extends Credentials {

    private static final long serialVersionUID = -8343864967200862794L;

    public static final String COUNTRYCODE = "countryCode";

    private String countryCode = "86";

    @NotNull
    private String username;

    @NotNull
    private String password;

    private boolean qrCode = false;

    public JigouCredentials() {

    }

    public JigouCredentials(String username, String countryCode, String password) {
        this.username = username;
        this.password = password;
        if (StringUtils.isNotBlank(countryCode)) {
            this.countryCode = countryCode;
        }
    }

    public JigouCredentials(String username, String countryCode, String password, boolean qrCode) {
        this(username, countryCode, password);
        this.qrCode = qrCode;
    }

    public final String getPassword() {
        return this.password;
    }

    public final void setPassword(final String password) {
        this.password = password;
    }

    @Override
    public final String getUsername() {
        return this.username;
    }

    public Integer getAccountType() {
        return AccountType.ORG.getCode();
    }

    public String getCountryCode() {
        return countryCode;
    }

    public boolean isQrCode() {
        return qrCode;
    }

    @Override
    public String toString() {
        return "JigouCredentials [accountType=" + getAccountType() + ", username=" + username + ", countryCode="
            + countryCode + ", password=**** ]";
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        JigouCredentials that = (JigouCredentials) o;

        if (password != null ? !password.equals(that.password) : that.password != null)
            return false;
        if (username != null ? !username.equals(that.username) : that.username != null)
            return false;
        if (countryCode != null ? !countryCode.equals(that.countryCode) : that.countryCode != null)
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        int result = username != null ? username.hashCode() : 0;
        result = 31 * result + (password != null ? password.hashCode() : 0);
        return result;
    }

    @Override
    public Integer getUserRole() {
        return UserRoleType.ORG.getId();
    }
}